home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / SocketClasses / Chat / Guest.m < prev    next >
Encoding:
Text File  |  1992-07-23  |  7.4 KB  |  166 lines

  1. /***************************************************************************
  2. *                                                                          *
  3. * Guest.m                                                                  *
  4. * Copyright 1992 by Nik A Gervae                                           *
  5. *                                                                          *
  6. * Part of an example using the Objective-C classes (SktSocketManager,      *
  7. * SktSocket, and SktSocketUser) which implement a convenient interface to  *
  8. * Berkeley stream sockets under NeXTSTEP(r).  See the accompanying class   *
  9. * specifications (files with a .rtf or .spec suffix) and the sources for   *
  10. * further information.                                                     *
  11. *                                                                          *
  12. * NeXTSTEP is a registered trademark of NeXT Computer, Inc.                *
  13. *                                                                          *
  14. ****************************************************************************
  15. *                                                                          *
  16. * LICENSE                                                                  *
  17. *                                                                          *
  18. * This program is free software; you can redistribute it and/or modify     *
  19. * it under the terms of the GNU General Public License as published by     *
  20. * the Free Software Foundation.                                            *
  21. *                                                                          *
  22. * The program and this makefile are distributed in the hope that it will   *
  23. * be useful, but are provided "AS IS" AND WITHOUT ANY WARRANTY; without    *
  24. * any express or implied warranty of MERCHANTABILITY or FITNESS FOR A      *
  25. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  26. * Any use or distribution of the program and documentation must include    *
  27. * appropriate copyrights to acknowledge Nik A. Gervae and the Free         *
  28. * Software Foundation, Inc.                                                *
  29. *                                                                          *
  30. * You should have received a copy of the GNU General Public License        *
  31. * along with this program; if not, write to the Free Software              *
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
  33. *                                                                          *
  34. ****************************************************************************
  35. *                                                                          *
  36. * VERSION HISTORY                                                          *
  37. *                                                                          *
  38. * Version numbers are simply dates in the form YYYYMMDD.  These represent  *
  39. * the date that version was finished.  Only significantly changed versions *
  40. * are reported here, or those versions requiring explanation of changes.   *
  41. * There may be many interim stages between dated versions.                 *
  42. *                                                                          *
  43. * DateVersion Primary Author  Notes                                        *
  44. * ----------- --------------- -------------------------------------------- *
  45. * 19920327    Nik A Gervae    First released version                       *
  46. *                                                                          *
  47. ***************************************************************************/
  48. #import <stdio.h>
  49. #import "chat.h"
  50. #import "Guest.h"
  51.  
  52. #define STATE_NEW      0
  53. #define STATE_CHATTING 1
  54.  
  55.  
  56. /***************************************************************************
  57. *                                                                          *
  58. * These are the constant strings used.  Feel free to translate them into   *
  59. * your favorite language.  Do be sure to keep all the % directives in      *
  60. * place, or change the code that accesses these strings.                   *
  61. *                                                                          *
  62. ***************************************************************************/
  63. #define STR_HiThere             "Hi there!  What\'s your name? "
  64. #define STR_NewChatter          "(%s) %s has joined the conversation.\n"
  65. #define STR_NameAndQuote        "%s> %s\n"
  66.  
  67.  
  68.  
  69. @implementation Guest
  70.  
  71. /***************************************************************************
  72. *                                                                          *
  73. * initWithSocket:                                                          *
  74. *                                                                          *
  75. * Initializes, notifies the chat server, and dumps a friendly greeting.    *
  76. *                                                                          *
  77. ***************************************************************************/
  78.  
  79. - initWithSocket:newSocket
  80. {
  81.   [super initWithSocket:newSocket];
  82.  
  83.   connectState = STATE_NEW;
  84.  
  85.   [self queueOutputString:STR_HiThere];
  86.   [socket flushOutput];
  87.  
  88.   [Global_Chatserver guestDidInit:self];
  89.  
  90.   return self;
  91.  
  92. } /*initWithSocket:*/
  93.  
  94. /***************************************************************************
  95. *                                                                          *
  96. * update                                                                   *
  97. *                                                                          *
  98. * If there's no name yet, get it. Otherwise get next line, add your name,  *
  99. * and send it off to everyone.                                             *
  100. *                                                                          *
  101. ***************************************************************************/
  102. - update
  103. {
  104.   char *inputLine;
  105.   char *buf;
  106.  
  107.   buf = NULL;
  108.  
  109.  /*
  110.   * If we don't have the name yet....
  111.   */
  112.   if (STATE_NEW == connectState) {
  113.  
  114.     /// Should do error checking here....
  115.     inputLine = [self nextInputLine];
  116.  
  117.     if (inputLine && *inputLine) {
  118.  
  119.       name = inputLine;
  120.       [Global_Chatserver log:STR_NewChatter, [[self class] name], name];
  121.       connectState = STATE_CHATTING;
  122.       return self;
  123.     }
  124.   }
  125.  
  126.  /*
  127.   * They're happily chatting.
  128.   */
  129.   else {
  130.  
  131.     /// Should do error checking here....
  132.     inputLine = [self nextInputLine];
  133.  
  134.     if (inputLine && *inputLine) {
  135.  
  136.       buf = (char *)malloc(10 + strlen(name) + strlen(inputLine));
  137.  
  138.       sprintf(buf, STR_NameAndQuote, name, inputLine);
  139.       [Global_Chatserver announce:buf except:self];
  140.  
  141.       free(inputLine);
  142.       free(buf);        // It's been copied elsewhere.
  143.       }
  144.   }
  145.   return self;
  146. }
  147.  
  148. /***************************************************************************
  149. *                                                                          *
  150. * -free                                                                    *
  151. *                                                                          *
  152. * Tell the chat server we're going away, and do the usual stuff.           *
  153. *                                                                          *
  154. ***************************************************************************/
  155. - free
  156. {
  157.   [Global_Chatserver guestWillFree:self];
  158.   if (name) free(name);
  159.   return [super free];
  160. }
  161.  
  162. @end /*interface Guest*/
  163.  
  164. /***************************************************************************
  165. ***************************************************************************/
  166.